#include "classical-problems.h"

#include <stdio.h>
#include <math.h>
#include <algorithm>

namespace pot {

void PrintSumSequence(long long i, long long k, long long n) {  
  printf("%d = ", n);  
  for (long long j = 0; j < k; ++j) {
    if (j != 0) printf(" + ");
    printf("%lld", i + j);
  }  
  printf("\n");  
}

bool CheckSumSequence(long long i, long long k, long long n) { 
  long long sum = 0;
  for (long long j = 0; j < k; ++j) {   
    sum += i + j;
  }
  if (sum != n) {
    printf("error: sum = %lld, n = %lld, i = %lld, k = %lld\n", 
           sum, n, i, k);
    return false;
  }
  return true;
}

int ContinuousSumSequenceUnRobust(long long N) {
  long long m = N / 2;
  int count = 0;
  for (long long i = 1; i <= m; ++i) {
    double k = -(i - 0.5) + sqrt(i * (i - 1) + 0.25 + 2 * N);
    long long ik = static_cast<long long>(k);
    if (ik == k) {
      if (CheckSumSequence(i, ik, N)) ++count;
    }
  }
  return count;
}

int ContinuousSumSequence(long long N) {
  int count = 0;
  for (long long i = 3; i <= N; i += 2) {
    if ((N % i) == 0) {
      long long m = i;
      long long k = 2 * N / m;
      if (k > m) std::swap(k, m);
      long long j = (m + 1 - k) / 2;
      if (CheckSumSequence(j, k, N)) ++count;
    }
  }
  return count;
}

long long NWithMaximumContinuousSumSequence() {
  /*long long N = 1;
  long long factor = 3;
  do {

  }*/
  return 0;
}

void ContinuousSumSequenceTests(long long N) {
  for (long long i = 1; i <= N; ++i) {
    int count = pot::ContinuousSumSequence(i);
    //printf("%d, count = %d\n", i, count);
    if (count == 0) printf("%d\n", i);
  }
}

} // pot
